package com.mac.macjetpackdemo

import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewTreeObserver
import android.widget.TextView
import com.mac.macjetpackdemo.util.QrCodeUtils
import com.mac.macjetpackdemo.view.ContainerBean
import com.mac.macjetpackdemo.view.MeetViewAdapter
import kotlinx.android.synthetic.main.activity_test.*
import kotlinx.coroutines.*
import java.lang.Exception
import java.lang.RuntimeException

class TestActivity : AppCompatActivity() {

    private val TAG = "TestActivity"
    val data = mutableListOf<ContainerBean>()
    lateinit var adapter: MeetViewAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        data.add(ContainerBean(1))
//        data.add(ContainerBean(2))
//        data.add(ContainerBean(3))
//        data.add(ContainerBean(4))
//        data.add(ContainerBean(5))
//        data.add(ContainerBean(6))
//        data.add(ContainerBean(7))
//        data.add(ContainerBean(8))
//        data.add(ContainerBean(9))
//        data.add(ContainerBean(10))
        adapter = object : MeetViewAdapter() {
            override fun getView(position: Int): View {
                val drawable = getDrawable(R.drawable.main_search_bg_shape)
                val textView = TextView(this@TestActivity)
                textView.background = drawable
                val text = mData[position].index
                textView.text = text
                textView.gravity = Gravity.CENTER
                return textView
            }
        }
        testContainer.setAdapter(adapter)
        adapter.setData(data)

//        testTry()

        initEvent()
        initQrCode()
    }

    private fun initQrCode() {
        val bitmap = QrCodeUtils.createQrCode("https://www.baidu.com")
        if (bitmap != null) {
            val addLogo = QrCodeUtils.addLogo(
                bitmap,
                BitmapFactory.decodeResource(resources, R.mipmap.cook_logo)
            )
            iv_qr_code.setImageBitmap(addLogo)
        }
    }

    private fun initEvent() {
        test_subtract.setOnClickListener {
            if (data.size > 0) {
                data.removeAt(0)
                adapter.setData(data)
            }
        }

        test_update.setOnClickListener {
            if (data.size > 0) {
                data.forEach {
                    it.index += " update"
                }
                adapter.setData(data)
            }
        }
        test_btn.setOnClickListener {
//            show()
            data.add(ContainerBean(data.size + 1))
            adapter.setData(data)
        }
    }

    private fun testTry() {
        try {
            System.currentTimeMillis()
            Log.e(TAG, "testTry: try")
            throw OutOfMemoryError()
            return
        } catch (e: Exception) {
            Log.e(TAG, "testTry: catch")
            return
        } finally {
            Log.e(TAG, "testTry: finally")
        }
    }

    fun test() {
        GlobalScope.launch(Dispatchers.Main) {
            Log.d(TAG, "start ->" + Thread.currentThread().name)
            print()
            Log.d(TAG, "end ->" + Thread.currentThread().name)
        }
    }

    suspend fun print() {
        withContext(Dispatchers.IO) {
            Log.d(TAG, "print ->" + Thread.currentThread().name)
        }
    }

    fun show() {
        GlobalScope.launch(Dispatchers.Main) {
            Log.d(TAG, "show ->" + Thread.currentThread().name)
            val request1 = async { request1(5) }
            val request2 = async { request2(6) }
            updateUi((request1.await() + request2.await()).toString())
        }
    }

    fun updateUi(result: String) {
        Log.d(TAG, "updateUi ->" + Thread.currentThread().name)
        test_btn.text = result
    }

    suspend fun request1(a: Int): Int {
        return withContext(Dispatchers.IO) {
            delay(1000)
            Log.d(TAG, "request1 ->" + Thread.currentThread().name)
            1 + a
        }
    }

    suspend fun request2(a: Int): Int {
        return withContext(Dispatchers.IO) {
            delay(500)
            Log.d(TAG, "request2 ->" + Thread.currentThread().name)
            a * 2
        }
    }
}
